package ciel.helper;

import java.util.ArrayList;

import org.mt4j.components.MTComponent;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.shapes.AbstractShape;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.widgets.MTImage;
import org.mt4j.components.visibleComponents.widgets.buttons.MTSvgButton;
import org.mt4j.input.gestureAction.InertiaDragAction;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.MTColor;
import org.mt4j.util.animation.Animation;
import org.mt4j.util.animation.AnimationEvent;
import org.mt4j.util.animation.IAnimationListener;
import org.mt4j.util.animation.MultiPurposeInterpolator;
import org.mt4j.util.math.Vector3D;

import ciel.res.CielSettings;
import ciel.scene.CielImageScene;
import ciel.utils.CielMath;

import processing.core.PApplet;
import processing.core.PGraphics;

public class CielHelper extends MTRectangle{
	
	public static final int FORWARD = 0;
	public static final int BACKWARD = 1;
	private int currentPosition = 0;

	ArrayList<MTImage> images;
	
	public CielHelper(PApplet pApplet, float width, float height) {
		super(pApplet, width, height);
		initUI();
		initArray();
		addButtons(pApplet);
	}
	
	private void initArray() {
		images = new ArrayList<MTImage>();
	}
	
	private void initUI() {
		setNoStroke(true);
		setFillColor(new MTColor(125, 125, 125, 100));
		setBoundsBehaviour(AbstractShape.BOUNDS_ONLY_CHECK );
		addGestureListener(DragProcessor.class, new InertiaDragAction(300, .97f, 17));
	}
	
	public void addHelpImage(MTImage img) {
		int i = images.size();
		img.setUserData("position", i);
		images.add(img);
		img.removeAllGestureEventListeners();
		if(i == 0) {
			currentPosition = 0;
			this.addChild(img);
			img.setWidthXYGlobal((float) (getWidthXYGlobal()*0.8));
			img.setPositionGlobal(getCenterPointGlobal());
			img.setPickable(false);
		}
	}
	
	public void changeHelpImage() {
		if(currentPosition < 5) {
			removeChild(images.get(currentPosition));
			currentPosition++;
			addChild(images.get(currentPosition));
			(images.get(currentPosition)).setWidthXYGlobal((float) (getWidthXYGlobal()*0.8));
			(images.get(currentPosition)).setPositionGlobal(getCenterPointGlobal());
			images.get(currentPosition).setPickable(false);
		} else {
			removeChild(images.get(currentPosition));
			currentPosition = 0;
			addChild(images.get(currentPosition));
		}
	}
	
	public void addButtons(PApplet pApplet) {
		MTSvgButton closeButton = new MTSvgButton(pApplet, "src/ciel/res/button_close_svg.svg");
		MTSvgButton forwardButton = new MTSvgButton(pApplet, "src/ciel/res/button_nect_svg.svg");
		
		forwardButton.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			
			@Override
			public boolean processGestureEvent(MTGestureEvent ge) {
				TapEvent te = (TapEvent)ge;
				if(te.isTapped()) {
					changeHelpImage();
				}
				return false;
			}
		});
		
		closeButton.addGestureListener(TapProcessor.class, new IGestureEventListener() {
			
			@Override
			public boolean processGestureEvent(MTGestureEvent ge) {
				TapEvent te = (TapEvent)ge;
				if(te.isTapped()) {
					removeFromParent();
				}
				return false;
			}
		});
		
		float factor = CielMath.getScaleDimension(25, closeButton.getWidthXYGlobal());
		closeButton.scale(factor, factor, factor, getCenterPointGlobal());
		forwardButton.scale(factor, factor, factor, getCenterPointGlobal());
		
		addChild(closeButton);
		addChild(forwardButton);
		
		float r = closeButton.getWidthXYRelativeToParent()/2;
		float x = getPosition(TransformSpace.GLOBAL).x + getWidthXY(TransformSpace.GLOBAL)/2;
		float y = getPosition(TransformSpace.GLOBAL).y - getHeightXY(TransformSpace.GLOBAL)/2;
		closeButton.setPositionGlobal(new Vector3D(x,y));
		y = getPosition(TransformSpace.GLOBAL).y + getHeightXY(TransformSpace.GLOBAL)/2;
		forwardButton.setPositionGlobal(new Vector3D(x,y));
	}
	
	public void drawComponent(PGraphics g) {
		super.drawComponent(g);
		Vector3D tmpVector = getCenterPointGlobal();
		if(tmpVector.x >= CielSettings.Dimensions.SCREEN_WIDTH) {
			setPositionGlobal(new Vector3D(0, tmpVector.y));
		} else if (tmpVector.x <= 0) {
			setPositionGlobal(new Vector3D(CielSettings.Dimensions.SCREEN_WIDTH, tmpVector.y));
		}
		if(tmpVector.y >= CielSettings.Dimensions.SCREEN_HEIGHT) {
			setPositionGlobal(new Vector3D(tmpVector.x, 0));
		} else if (tmpVector.y <= 0) {
			setPositionGlobal(new Vector3D(tmpVector.x, CielSettings.Dimensions.SCREEN_HEIGHT));
		}
	}

}
